home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / FWPriMem.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  4.6 KB  |  158 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWPRIMEM_H
  13. #include "FWPriMem.h"
  14. #endif
  15.  
  16. #ifndef SLPRIDEB_H
  17. #include "SLPriDeb.h"
  18. #endif
  19.  
  20. #ifdef FW_BUILD_MAC
  21. #pragma segment FWCommon
  22. #endif
  23.  
  24. typedef void (*pfvv)();
  25. extern pfvv set_new_handler(pfvv);
  26.  
  27. //========================================================================================
  28. // set_new_handler
  29. //========================================================================================
  30.  
  31. // We (optionally) provide our own definition of set_new_handler mainly as a precaution against
  32. // ending up with a shared library version of set_new_handler.  ODF as a whole has been
  33. // written assuming that operator new will throw an exception if it fails to allocate
  34. // memory.  In a runtime environment such as OpenDoc, it's possible for multiple shared
  35. // libraries to exist in one process.  An exception thrown from code in one shared
  36. // library should not propogate outside of the shared library (since there is no agreed
  37. // binary interface for exceptions across shared libraries).  This implies that if
  38. // operator new throws exceptions then operator new cannot be implemented in a shared
  39. // library.  In actuality, it's the "new hander" that throws, so there must be one new
  40. // handler per shared library, and thus one version of set_new_handler per shared library.
  41. // This unit (FWPriMem.cpp) is intended to be statically linked into each shared library
  42. // built with ODF.
  43.  
  44. #if defined(FW_DONT_USE_STD_LIB_SET_NEW_HANDLER)
  45.  
  46.     static pfvv gNewHandler = 0;
  47.     
  48.     #ifndef _MSC_VER
  49.         pfvv set_new_handler(pfvv handler)
  50.         {
  51.             // See ARM, pp 280-81.
  52.             pfvv oldHandler = gNewHandler;
  53.             gNewHandler = handler;
  54.             return oldHandler;
  55.         }
  56.     #endif
  57.     
  58.     #ifdef _MSC_VER
  59.     
  60.         #include <new.h>
  61.         
  62.         pfvv __cdecl set_new_handler(pfvv handler)
  63.         {
  64.             // See ARM, pp 280-81.
  65.             pfvv oldHandler = gNewHandler;
  66.             gNewHandler = handler;
  67.             return oldHandler;
  68.         }
  69.         
  70.         // [KVV] Because of the way Visual C++ 4.0 linker/library work,
  71.         //    we have to define the following stuff
  72.         
  73.         _PNH _set_new_handler(_PNH /* pnh */)
  74.         {
  75.             return 0;
  76.         }
  77.         
  78.         _PNH _query_new_handler()
  79.         {
  80.             return 0;
  81.         }
  82.         
  83.         extern "C" int _callnewh(size_t /* size */)
  84.         {
  85.             return 1;
  86.         }
  87.     
  88.     #endif
  89.  
  90. #endif
  91.  
  92. //========================================================================================
  93. // C++ Global operator new & delete
  94. //========================================================================================
  95.  
  96. //----------------------------------------------------------------------------------------
  97. // operator new, placed memory version
  98. //----------------------------------------------------------------------------------------
  99.  
  100. // already defines placement new in <new.h>
  101. //    Turn off definition if we ought not define it
  102. #define FW_NEW_WITH_PLACEMENT
  103.  
  104. #if defined(_MSC_VER)
  105. #undef    FW_NEW_WITH_PLACEMENT
  106. #elif defined(SYMANTEC_CPLUS)
  107. #undef    FW_NEW_WITH_PLACEMENT
  108. #elif defined(__MWERKS__)
  109. #if __option(dont_inline)
  110. #undef    FW_NEW_WITH_PLACEMENT
  111. #endif
  112. #endif
  113.  
  114. #ifdef FW_NEW_WITH_PLACEMENT
  115. void *operator new(size_t /* size */,void *p)
  116. {
  117.     return p;
  118. }
  119. #endif
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // operator new
  123. //----------------------------------------------------------------------------------------
  124. void *operator new(size_t size)
  125. {
  126.     void* p;
  127.     
  128.     while ((p = FW_PrimitiveAllocateBlock(size)) == NULL)
  129.     {
  130.         pfvv newHandler;
  131.         // this is just a cheap way to get the newHandler function pointer
  132.         set_new_handler(newHandler = set_new_handler(0));
  133.         FW_PRIV_ASSERT(newHandler != NULL);
  134.         // ODF is written assuming that operator new will not return if it fails to
  135.         // allocate memory.  It is assumed that a newHandler will be installed which
  136.         // throws an exception.  ODF installs a newHandler in a higher layer (FWODExce)
  137.         // so a newHandler will exist unless a developer chooses to intentionally
  138.         // deinstall the newHandler by calling ::set_new_handler(0).  Doing so will
  139.         // break ODF's error handling strategy and is considered illegal.  Note
  140.         // that developers are free to install their own newHandler.
  141.         if (!newHandler)
  142.             return 0;
  143.         else
  144.             newHandler();
  145.     }
  146.  
  147.     return p;
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // operator delete
  152. //----------------------------------------------------------------------------------------
  153. void operator delete(void *p)
  154. {
  155.     FW_PrimitiveFreeBlock(p);
  156. }
  157.  
  158.